/**
 * 遍历一遍找到删除节点位置
 */

const removeNthFromEnd = (head, n) => {
  if (head == null || head.next == null) return null
  const newHead = new ListNode(0)
  newHead.next = head
  let length = 1, curr = newHead, newCurr = newHead
  while (curr.next) {
    curr = curr.next
    length++
  }

  let ans = length - n, i = 0
  console.log(length - n);
  while (newCurr.next) {
    if (i === ans - 1) {
      newCurr.next = newCurr.next.next
    } else {
      newCurr = newCurr.next
    }
    i++
  }
  return newHead.next
}

const head = {
  val: 1,
  next: {
    val: 2,
    // next: null
    next: {
      val: 3,
      next: {
        val: 4,
        next: {
          val: 5,
          next: {
            val: 6,
            next: null
          }
        }
      }
    }
  }
}

console.log(removeNthFromEnd(head, 6));
